package com.yulang.springbootnetty.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yulang.springbootnetty.bo.UsersBO;
import com.yulang.springbootnetty.pojo.ChatMsg;
import com.yulang.springbootnetty.pojo.FriendsRequest;
import com.yulang.springbootnetty.pojo.Users;
import com.yulang.springbootnetty.service.FriendsRequestService;
import com.yulang.springbootnetty.service.UsersService;
import com.yulang.springbootnetty.utils.ChatMsgs;
import com.yulang.springbootnetty.utils.JSONResult;
import com.yulang.springbootnetty.utils.MD5Utils;
import com.yulang.springbootnetty.vo.FriendRequestVO;
import com.yulang.springbootnetty.vo.MyFriendsVO;
import com.yulang.springbootnetty.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/u")
public class UsersController {


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private Sid sid;
    
    @Autowired
    private UsersService usersService;

    @Autowired
    private FriendsRequestService friendsRequestService;

    @PostMapping("/registOrLogin")
    public JSONResult registOrLogin(@RequestBody Users users) throws Exception {
        if (StringUtils.isEmpty(users.getUsername()) || StringUtils.isEmpty(users.getPassword())) {
            return JSONResult.errorMsg("用户名或密码不能为空！");
        }

        boolean isExits = usersService.nameIsExits(users.getUsername());
        if (isExits) {
            //1.1登录
            users = usersService.queryUserForLogin(users.getUsername(), MD5Utils.getMD5Str(users.getPassword()));
            if (users == null) {
                return JSONResult.errorMsg("用户名或密码错误！");
            }
        } else {
            //注册
            usersService.saveUser(users);
        }
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(users,vo);
        return JSONResult.ok(vo);
    }


    @PostMapping("/uploadFaceBase64")
    public JSONResult uploadBase64(@RequestBody UsersBO usersBO) throws Exception {
        String base64Data = usersBO.getFaceData();
        String sids = sid.nextShort();
        File file = new File("D:\\springboot-netty\\src\\main\\resources\\static\\"+ usersBO.getUserId()+"/"+sids + "/userface64.png");
        Users users = new Users();
        users.setFaceImage("/"+usersBO.getUserId() +"/"+sids+ "/userface64.png");
        users.setFaceImageBig("/"+usersBO.getUserId()+ "/" + sids + "/userface64.png");
        users.setId(usersBO.getUserId());
        usersService.updateById(users);
        users = usersService.getById(users.getId());
        return JSONResult.ok(users);
    }


    @PostMapping("/setNickname")
    public JSONResult setNickname(@RequestBody Users users) throws Exception {
        usersService.updateById(users);
        return JSONResult.ok(users);
    }


    @PostMapping("/search")
    public JSONResult search( String myUserId,String friendUsername) throws Exception {
        QueryWrapper<Users> query = new QueryWrapper<>();
        query.eq(Users.USERNAME,friendUsername);
        Users user = usersService.getOne(query);
        if(user==null){
            return JSONResult.errorMsg("没有这个用户");
        }
        if(user.getId().equals(myUserId)){
            return JSONResult.errorMsg("不能添加自己为好友");
        }
        return JSONResult.ok(user);
    }

    @PostMapping("/addFriendRequest")
    public JSONResult addFriendRequest( String myUserId,String friendUsername) throws Exception {
        QueryWrapper<Users> query = new QueryWrapper<>();
        query.eq(Users.USERNAME,friendUsername);
        Users user = usersService.getOne(query);
        if(user==null){
            return JSONResult.errorMsg("没有这个用户");
        }
        if(user.getId().equals(myUserId)){
            return JSONResult.errorMsg("不能添加自己为好友");
        }

        QueryWrapper<FriendsRequest> queryRequest = new QueryWrapper<>();
        queryRequest.eq(FriendsRequest.SEND_USER_ID,myUserId);
        queryRequest.eq(FriendsRequest.ACCEPT_USER_ID,user.getId());
        FriendsRequest requestFriend = friendsRequestService.getOne(queryRequest);

        if(requestFriend!=null){
            return JSONResult.errorMsg("不要重复添加");
        }

        usersService.addFriend(myUserId,user.getId());

        return JSONResult.ok(user);
    }



    @PostMapping("/queryFriendRequests")
    public JSONResult addFriendRequest( String userId) throws Exception {
        List<FriendRequestVO> list = usersService.friendRequest(userId);
        return JSONResult.ok(list);
    }


    @PostMapping("/operFriendRequest")
    public JSONResult operFriendRequest( String acceptUserId,String sendUserId,Integer operType) throws Exception {
        List<MyFriendsVO> list = usersService.operFriendRequest(acceptUserId, sendUserId, operType);
        return JSONResult.ok(list);
    }

    @PostMapping("/myFriends")
    public JSONResult myFriends( String userId) throws Exception {
        List<MyFriendsVO> list = usersService.myFriends(userId);
        return JSONResult.ok(list);
    }

    @PostMapping("/getUnReadMsgList")
    public JSONResult getUnReadMsgList( String acceptUserId) throws Exception {
        List<ChatMsgs> list = usersService.listMsg(acceptUserId);
        return JSONResult.ok(list);
    }



}

